<?php
class M_barangstok extends CI_Model{
	
	function get($end,$start,$filter){
		$sql = "SELECT * FROM (
				SELECT barang_nama, IF(barang_jenis = 1, 'Habis Pakai','Aset') as barang_jenis, barang_kode, barang_merk , SUM(qty) as barang_stok FROM (
				SELECT m_barang_id, barangmasukdet_ket, barangmasukdet_qty as qty FROM t_barangmasukdet
				LEFT JOIN t_barangmasuk ON t_barangmasuk_id = barangmasuk_id
				WHERE barangmasuk_id is not null
				UNION 
				SELECT m_barang_id, barangkeluardet_ket, barangkeluardet_qty * -1 as qty FROM t_barangkeluardet
				LEFT JOIN t_barangkeluar ON t_barangkeluar_id = barangkeluar_id
				WHERE barangkeluar_id is not null
				) t_barangstok  
				LEFT JOIN m_barang ON m_barang_id = barang_id";
		$sql .= " GROUP BY m_barang_id ) t ";
		
		if ($filter<>""){
		//		$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " WHERE (lower(`barang_nama`) LIKE '%".addslashes(strtolower($filter))."%' )"; 
		}
		
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		
		
		$sql .= " ORDER BY barang_nama ";
		$limit = $sql." LIMIT ".$start.", ".$end;
		$query = $this->db->query($limit);
		$arr = array();
		foreach($query->result() as $q){
			$arr[] = $q;
		}
	
		$final = array(
			'total'   => $total,
			'results' => $arr
		);
		return json_encode($final);
	}
	/*
	function save($data){
		extract($data);
		$data_submit = array(
			'barangstok_date' => $barangstok_date,
			'barangstok_ket'   => $barangstok_ket,
			'm_jeniskegiatan_id' => $m_jeniskegiatan_id,
			't_pengajuandet_id' => $t_pengajuandet_id,
			'm_anggaran_id' => $m_anggaran_id
		);
		$this->db->insert('t_barangstok',$data_submit);
		$t_barangstok_id = $this->db->insert_id();
		for($i = 0; $i < count($barangstok_grid[0]); $i++){
			$data_submit = array(
				't_barangstok_id' =>$t_barangstok_id ,
				'm_barang_id' => $barangstok_grid[0][$i],
				'barangstokdet_qty' => $barangstok_grid[1][$i],
				'barangstokdet_price' => $barangstok_grid[2][$i],
				'barangstokdet_ket' => $barangstok_grid[3][$i]
			);
			$this->db->insert('t_barangstokdet',$data_submit);
		}
		echo 1;
	}
	
	function update($data){
		extract($data);
		$data_submit = array(
			'barangstok_date' => $barangstok_date,
			'barangstok_ket'   => $barangstok_ket,
			'm_jeniskegiatan_id' => $m_jeniskegiatan_id,
			't_pengajuandet_id' => $t_pengajuandet_id,
			'm_anggaran_id' => $m_anggaran_id
		);
		$this->db->where('barangstok_id',$barangstok_id);
		$this->db->update('t_barangstok',$data_submit);
		$t_barangstok_id = $barangstok_id;
		
		$this->db->where('t_barangstok_id',$t_barangstok_id);
		$this->db->delete('t_barangstokdet');
		
		for($i = 0; $i < count($barangstok_grid[0]); $i++){
			$data_submit = array(
				't_barangstok_id' =>$t_barangstok_id ,
				'm_barang_id' => $barangstok_grid[0][$i],
				'barangstokdet_qty' => $barangstok_grid[1][$i],
				'barangstokdet_price' => $barangstok_grid[2][$i],
				'barangstokdet_ket' => $barangstok_grid[3][$i]
			);
			$this->db->insert('t_barangstokdet',$data_submit);
		}
		echo 1;
	}
	
	function delete($data){
		$sql = "DELETE FROM t_barangstok WHERE barangstok_id = ".$data;
		$this->db->query($sql);
		echo 1;
	} */

}